/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

/**
 *
 * @author QuyetNM1
 */
public class MIDletGame extends MIDlet {
//implements IAPClientPaymentListener {

    public static final String PURCHASE_ID = "884680";
    public static String PRICE = "0";
    public static boolean purchaseStatus = false;
    private static CanvasGame canvas;
    private static Display display;
//    private Tracker tracker;
//    private static IAPClientPaymentManager pManager;

    public MIDletGame() {
        canvas = new CanvasGame(this);
//        tracker = Tracker.getInstance(this, "UA-30587251-1");
//        tracker.addToQueue(new PageView("DuoiHinhBatChu"));
//        tracker.flush(true);

    }

    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(canvas);
//        pManager = getIAPManager();
//        pManager.setIAPClientPaymentListener(this);
//        if (Map.levelMax < 19) {
//            pManager.getProductData(PURCHASE_ID);
//        }
//        int status = pManager.getRestorableProducts(IAPClientPaymentManager.ONLY_IN_SILENT_AUTHENTICATION);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
        exit();
    }

    public void exit() {
        canvas = null;
        notifyDestroyed();
    }

    public static Display getDisplay() {
        return display;
    }

//    public static boolean purchaseNextLevel() {
//        int status = pManager.purchaseProduct(PURCHASE_ID,
//                IAPClientPaymentManager.FORCED_AUTOMATIC_RESTORATION);
//        if (status != IAPClientPaymentManager.SUCCESS) {
//            showAlertMessage(display, "Purchase failure", "Purchase process failed. "
//                    + Messages.getPaymentError(status), AlertType.ERROR);
//            return false;
//        }
//        return true;
//    }
//
//    public static boolean restoreNextLevel() {
//        int status = pManager.restoreProduct(PURCHASE_ID,
//                IAPClientPaymentManager.ONLY_IN_SILENT_AUTHENTICATION);
//        if (status != IAPClientPaymentManager.SUCCESS) {
//            showAlertMessage(display, "Restoration failure", "Restoration process failed. "
//                    + Messages.getPaymentError(status), AlertType.ERROR);
//            return false;
//        }
//        return true;
//    }
//
//    // Functions necessary to implement IAPClientPaymentListener interface
//    public void userAndDeviceDataReceived(int status, IAPClientUserAndDeviceData ud) {
//    }
//
//    public void restorableProductsReceived(int status, IAPClientProductData[] productDataList) {
//    }
//
//    public void productDataListReceived(int status, IAPClientProductData[] productDataList) {
//    }
//
//    public void productDataReceived(int status, IAPClientProductData pd) {
//        if (status == OK) {
//            PRICE = pd.getPrice();
//        }
//    }
//
//    public void purchaseCompleted(int status, String purchaseTicket) {
//        if (status == OK) {
////            tracker.addToQueue(new PageView("DuoiHinhBatChu_PurchaseComplete " + PRICE));
////            tracker.flush(true);
//            purchaseStatus = true;
//            Map.levelMax += 2;
//            canvas.getRMS().open();
//            canvas.getRMS().saveLevelMax(Map.levelMax + 1);
//            canvas.getRMS().close();
//            canvas.getMap().initLevelMap();
//        } else {
//            showAlertMessage("Purchase failure", "Purchase process failed. "
//                    + Messages.getPaymentError(status), AlertType.ERROR);
//        }
//    }
//
//    public void restorationCompleted(int status, String purchaseTicket) {
//        if (status == OK) {
////            tracker.addToQueue(new PageView("DuoiHinhBatChu_PurchaseComplete " + PRICE));
////            tracker.flush(true);
//            purchaseStatus = true;
//            Map.levelMax += 2;
//            canvas.getRMS().open();
//            canvas.getRMS().saveLevelMax(Map.levelMax + 1);
//            canvas.getRMS().close();
//            canvas.getMap().initLevelMap();
//        } else {
//            showAlertMessage("Restoraiton failure", "Restoration failed. "
//                    + Messages.getPaymentError(status), AlertType.ERROR);
//        }
//    }
//
//    public static IAPClientPaymentManager getIAPManager() {
//        if (pManager == null) {
//            try {
//                pManager = IAPClientPaymentManager.getIAPClientPaymentManager();
//            } catch (IAPClientPaymentException ipe) {
//            }
//        }
//        return pManager;
//    }
//
//    public void showAlertMessage(String title, String alertText, AlertType type) {
//        showAlertMessage(display, title, alertText, type);
//    }
//
//    public static void showAlertMessage(Display display, String title,
//            String alertText, AlertType type) {
//        Alert alert = new Alert(title, alertText, null, type);
//        display.setCurrent(alert, display.getCurrent());
//    }

//    public Tracker getTracker() {
//        return tracker;
//    }
//
//    public void setTracker(Tracker tracker) {
//        this.tracker = tracker;
//    }
    
}
